<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcrew extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getCrewDB($number,$offset)
    {
        //$id = $this->getParamUri(3,2);
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT c.* FROM crew AS c WHERE 1=1 ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.fullname like '%".$key."%' OR ";
            $sql .= " c.rank like '%".$key."%' OR ";
            $sql .= " c.email like '%".$key."%' ) ";
        }
        $sql .= "ORDER BY c.crew_id ASC";
        
        $xdata = " LIMIT ".$limit.",".$number." ";
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function addCrewDb($img)
    {
        $name_crew            = $this->getParamString('nameCrew');
        
        $data = array(
                    'fullname'              => $name_crew,
                    'rank'                  => $this->getParamString('rank'),
                    'img'                   => $img,
                    'date_of_birth'         => $this->getParamString('date-of-birth'),
                    'sex'                   => $this->getParamString('sex'),
                    'address'               => $this->getParamString('address'),
                    'email'                 => $this->getParamString('email'),
                    'mobile'                => $this->getParamString('mobile'),
                    'signed_on_date'        => $this->getParamString('signed-on-date'),
                    'seaman_pass_no'        => $this->getParamString('seaman-pass-no'),
                    'seaman_pass_exp_date'  => $this->getParamString('seaman-pass-exp-date'),
                    'intl_pass_no'          => $this->getParamString('intl-pass-no'),
                    'intl_pass_exp_date'    => $this->getParamString('intl-pass-exp-date'),
                    'note_crew'             => $this->getParamString('note-crew')
                    );
        
        $this->db->insert('crew', $data);
        
        return 200;
    }
    
    public function editCrewDb($id,$img)
    {
        $name_crew            = $this->getParamString('nameCrew');
        
        $data = array(
                    'fullname'              => $name_crew,
                    'rank'                  => $this->getParamString('rank'),
                    'img'                   => $img,
                    'date_of_birth'         => $this->getParamString('date-of-birth'),
                    'sex'                   => $this->getParamString('sex'),
                    'address'               => $this->getParamString('address'),
                    'email'                 => $this->getParamString('email'),
                    'mobile'                => $this->getParamString('mobile'),
                    'signed_on_date'        => $this->getParamString('signed-on-date'),
                    'seaman_pass_no'        => $this->getParamString('seaman-pass-no'),
                    'seaman_pass_exp_date'  => $this->getParamString('seaman-pass-exp-date'),
                    'intl_pass_no'          => $this->getParamString('intl-pass-no'),
                    'intl_pass_exp_date'    => $this->getParamString('intl-pass-exp-date'),
                    'note_crew'             => $this->getParamString('note-crew')
                    );
                    
        if(!in_array($img, array(null, '', '0')))
        {
            $data['img'] = $img;
            // Xu ly them phan xoa anh
        }
        
        $this->db->update('crew', $data,"crew_id = ".$id."");
        return 200;
    }
    
    public function getCrewEdit($id)
    {
        $var = $this->db->query('SELECT * FROM crew WHERE crew_id = '.$id)->row_object();
        return $var;
    }
    
    public function deleteCrewDB()
    {
        $id = $this->getParamInt('id');
        $this->db->delete('crew', "crew_id = ".$id);
        return 200;
    }
    
    public function sexOption($opt = 0)
    {
        $arr = array(   0 =>"-- Select --",
                        1 => "Male",
                        2 => "Female"
                    );
        $html  = '';
        
        foreach($arr AS $key=>$val)
        {
            
            if($opt == $key)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$key.'" '.$selected.'>'.$val.'</option>';
        }
        return $html;
    }
    
    
    
    
}